package inf.coach.mobile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class CommentList extends Activity  {
	
	public static long userid = 0;
	public static long osm_id = 0;
	public static TextView info = null;
	public static ListView list = null;
	public static int num = 0;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.commentation);
		setTitle("kommentar Seite, Anzeige des Suchergebnisses");

		//info = (TextView) findViewById(R.id.info_poi);

		Bundle bundle = this.getIntent().getExtras();
		userid = bundle.getLong("userid");
		osm_id = bundle.getLong("osmid");
		
		list = (ListView) findViewById(R.id.listView_comment);
		info = (TextView) findViewById(R.id.textView_commen_info);
		
		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
		{
//			List<comment> newlistview=webservice.getCommentList(2222);
			List<comment> newlistview=webservice.getCommentList(userid,osm_id);
			Collections.sort(newlistview,new Comparator<comment>(){   
		           public int compare(comment arg0, comment arg1) {   
		               return arg0.date.compareTo(arg1.date);   
		            }   
		        });  
			Collections.reverse(newlistview);
			if (!newlistview.isEmpty()) {
				listview(newlistview);
				info.setText("Insgesamt "+ num +" Ergebnisse gefunden!");
				}
			else{
				
				info.setText("Insgesamt "+ num +" Ergebnisse gefunden!");
				
				System.out.println("commentlist null");
				}
			}
			else{
				message.show(CommentList.this,"Fehler", "Keine Internetverbindung !");
				//mTts.speak("Information Window, Error, There is no Internet Connection , Ok button", TextToSpeech.QUEUE_FLUSH, null);
			}
		}
	
	
	public void listview(final List<comment> comments) {

		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		if (!comments.equals(null)) {
			num = comments.size();			
			View headerView = getLayoutInflater().inflate(R.layout.listview,
					null);

			for (int i = 0; i < num; i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				comment comment = (comment) comments.get(i);
				String type;
				if(comment.type.toString().equals("1"))
				{
					type="text";
				}
				else
				{
					type="Audio";
				}
				map.put("Autor", (i+1)+". Autor: "+comment.author);
/*				map.put("A Building", "Amenity: "+apoi.amenity+" Building: " + apoi.building);*/
				map.put("Type", "Typ: "+type+", Datum: " + comment.date.substring(0, 10));
				mylist.add(map);
			}

			SimpleAdapter mSchedule = new SimpleAdapter(this, mylist,// 数据来源
					R.layout.listview,// ListItem的XML实现

					// 动态数组与ListItem对应的子项
					new String[] { "Autor", "Type" },

					// ListItem的XML文件里面的两个TextView ID

					new int[] { R.id.listview_title, R.id.listview_info });

			mSchedule.notifyDataSetChanged();
			// 添加并且显示

			list.setAdapter(mSchedule);

			// list.addHeaderView(headerView);

			list.setItemsCanFocus(true);

			list.setSelectionAfterHeaderView();

			list.setOnItemClickListener(new OnItemClickListener() {


				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();

					Bundle bundle = new Bundle();
					comment comment = (comment) comments.get(arg2);
					bundle.putLong("userid", userid);
					bundle.putLong("osmid", osm_id);
					bundle.putLong("comment_id", comment.comment_id);
					
					if(comment.type.toString().equals("1"))
					{
					intent.setClass(CommentList.this, CommentText.class);
					intent.putExtras(bundle);
					startActivity(intent);
					}
					else
					{
						intent.setClass(CommentList.this, CommentAudio.class);
						intent.putExtras(bundle);
						startActivity(intent);
					}
					
					
				}

			});

			
		}
	}
	
    protected void dialoggoback() { 
        AlertDialog.Builder builder = new Builder(CommentList.this); 
        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
        builder.setTitle("Information"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        CommentList.this.finish(); 
                    }

                }); 
        builder.setNegativeButton("abbrechen", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialoggoback(); 
            return false; 
        } 
        return false; 
    } 
	}


